package com.condroid;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;



/**
 * This is StdBysetting class and it contains different
 * methods to be invoked by other classes related to stdby 
 * settings.
 * @author Yasir Ali & Jun gao
 * Condroid Summer CSD 2011 - KTH
 */
public class StdBySettings {
	
	
	public static SharedPreferences newstdByInstance;
	public static String temp;
	public static SharedPreferences.Editor AllowNo;
	private static String[] loadedData = new String[128];
	private static int count;
	public static SharedPreferences.Editor AllowAccess;
	public static String temp1;
	private static String number;
	
	
	/**
	 * Not used!!
	 * @param num
	 * @param code
	 */
	StdBySettings(int num, int code){
		
	// Contructor !! use if required!
		
	}

	
// Set the numbers to be allowed
	/***/
	
	/**
	 * This methods checks whether the number is in the storage
	 * or not, it can be invoked by different classes
	 * @param num
	 * @return oolean
	 */
	public static boolean checkAllowNo(String num) {
		temp = newstdByInstance.getString("AllowNo", "Not able to Load");
		loadedData = makeCommand(temp);
		
		for (count=0;count<loadedData.length;count++){
			
			if (loadedData[count].equalsIgnoreCase(num)){
					return true;
			} 
		}
		
		
		return false;
	}
	
	
	/***/
	
	/**
	 * This methods deletes the number from the storage
	 * it can be invoked by different classes
	 * @param num
	 * @return boolean
	 */
	public static boolean delAllowNo(String num) {
		temp = newstdByInstance.getString("AllowNo", "Not able to Load");
		loadedData = makeCommand(temp);
				for (count=0;count<loadedData.length;count++){
					
					if (loadedData[count].equalsIgnoreCase(num)){
							loadedData[count]=null;
							temp="";
							for(int i=0;i<loadedData.length;i++) {
							if(loadedData[i]!=null)
							temp+=(loadedData[i]+";");
							}
							AllowNo = newstdByInstance.edit();
							AllowNo.putString("AllowNo", temp);
							AllowNo.commit();
							return true;
							
					} 
				}
				
				
				return false;
	}
	
	/***/
	
	/**
	 * This methods adds the number in the storage
	 * it can be invoked by different classes
	 * @param num
	 * @return boolean
	 */
	public static boolean addNumber(String num) {
		temp = newstdByInstance.getString("AllowNo", "Not able to Load");
		loadedData = makeCommand(temp);
				for (count=0;count<loadedData.length;count++){
					
					if (loadedData[count].equalsIgnoreCase(num)){
							return false;
					}
					/*	y=loadedData.length+1;
						String[] loadedDatatemp = new String[y];
								
						temp="";
						for(int i=0;i<loadedData.length;i++) {
						if(loadedData[i]!=null)
						loadedDatatemp[i]=loadedData[i];
						temp+=(loadedDatatemp[i]+";");
						}
						loadedDatatemp[y]=num;
						temp+=loadedDatatemp[y];*/
						temp+=(num);
						
						AllowNo = newstdByInstance.edit();
						AllowNo.putString("AllowNo", temp+";");
						AllowNo.commit();
						return true;
							
				} 
				
				
				
				return false;
	}


	

	/***/
	
	/**
	 * check the passwords to be allowed
	 * @param code
	 * @return boolean
	 */
	
	public static boolean checkAccessCode(String code) {
		temp = newstdByInstance.getString("AllowAccess", "Not able to Load");
		if (temp.equals(code)) {
			return true;
		}
		return false;

	}
	
	/***/
	
	
	/**
	 * Give privileges to other classes and activity to use these shared 
	 * preferences (This method needs to be called in order to use any
	 * of the methods in this class!!).
	 * @param context
	 * @return
	 */
	public static String getConfiguration(Context context){
		
		newstdByInstance = PreferenceManager.getDefaultSharedPreferences(context);
		newstdByInstance = context.getSharedPreferences(StdbyActivity.stdByFile,0);
		
		temp = newstdByInstance.getString("AllowNo", "Not able to Load");
		temp1 = newstdByInstance.getString("AllowAccess", "Not able to Load");

		return temp+ '/' +temp1;
					
		}
		
	
	/***/
	/** Check whether the user a number or not
	 * @param allowN
	 * @return boolean
	 */
	private static boolean checkNumber(String allowN) {
			
		try {
			Long.parseLong(allowN);
			return true;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return false;

		}
	}
		
	/***/
	
	/** Commit configuraiton by using other methods
	 * @param context
	 * @return
	 */
	public static boolean commitConfiguration(String allowData2,String accessCode) {
		if (allowData2.length() != 0 && accessCode.length() != 0) {
			if (checkConfig(allowData2)) {
				loadedData = makeCommand(allowData2);
				// check for null values
				count = 0;
				for (String f : loadedData) {
					if (loadedData[count] == null) {
						return false;
					}
					count++;
				}
				count = 0;
				for (count = 0; count < loadedData.length; count++) {
					if(loadedData[count].startsWith("+")){
						number=loadedData[count].substring(1);
					if (checkNumber(number)) {

						continue;
					} else
						return false;
					}
				}

				AllowNo = newstdByInstance.edit();
				AllowNo.putString("AllowNo", allowData2);
				AllowNo.commit();
				AllowNo = newstdByInstance.edit();
				AllowNo.putString("AllowAccess", accessCode);
				AllowNo.commit();
				return true;
			}else return false;

		}else return false;

	}
		private static String[] makeCommand(String allowData2) {

			loadedData = allowData2.split(";"); // Split user text by ";"

			boolean isCorrect = false;
			count=0;
			for (String x: loadedData){ // For loop to check each segment syntax
		     
		            isCorrect = checkSyntax(loadedData[count]); 
		            // checks syntax
		            if (isCorrect != true){
		            	loadedData[count]=null;
		            	return loadedData;
		            }
		            
		        count++;
			}
			return loadedData;	
			}

		
		
		public static boolean checkSyntax(String checkValue) {
			
			boolean isCorrect = false;
			int z=0;
			int len = checkValue.length();
				for (z=0;z<+len;z++){
					char c=checkValue.charAt(z);
						if( Character.isUpperCase(c) 
						|checkValue.charAt(z) == '*' 
						|checkValue.charAt(z) == '&'
						|checkValue.charAt(z) == '-' 
						|checkValue.charAt(z) == '/' 
						|checkValue.charAt(z) == '?' 
						|checkValue.charAt(z) == '#' ){
						isCorrect = false;
						break;
						}else {
						isCorrect = true;
						

					}
					
				}
				
			return isCorrect;
		}

		
		
		

		/***/
	

		/**
		 * Checks whether the!! statement are correctly formatted or not!!
		 * @param allowData2
		 * @return
		 */
		private static boolean checkConfig(String allowData2) {
				if (allowData2.endsWith(";")){
					return true;
				}
				return false;
				
		}
		
		
}
	
	
		


